package Network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class HomeworkTcpEchoClient {
    private Socket socket = null;

    public HomeworkTcpEchoClient(String serverIP,int port) throws IOException {
        socket = new Socket(serverIP,port);
    }

    public void start(){
        Scanner scanner = new Scanner(System.in);

        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()){
            Scanner scannerNet = new Scanner(inputStream);
            PrintWriter writer = new PrintWriter(outputStream);

            while (true){
                // 1.从控制台读取用户输入
                String request = scanner.next();
                // 2.发送给服务器
                writer.println(request);
                writer.flush();
                //3.从服务器读取响应
                String response = scannerNet.next();

                System.out.println(response);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        HomeworkTcpEchoClient client = new HomeworkTcpEchoClient("127.0.0.1",9090);
        client.start();
    }

}
